var crypto = require('crypto');

(function () {
    //md5加密
    var encryPassword = this.encryPassword = function (password) {
        password = 'tao1024' + password;
        var md5 = crypto.createHash('md5');
        md5.update(password);
        password = md5.digest('hex');
        return password;
    };
    //将推荐码转为uid 注意，修改时usuz_srv也要改
    var codeToUid = this.codeToUid = function(str) {
        var set = 'qwertyuiopasdfghjklzxcvbnm';
        var a = str.toLocaleLowerCase();
        a = str.split('');
        var index5 = set.indexOf(a[0]);
        var index1 = set.indexOf(a[1]);
        var index2 = set.indexOf(a[2]);
        var index3 = set.indexOf(a[3]);
        var index4 = set.indexOf(a[4]);
        var verifyCode = (index1 + index2 + index3 + index4) % 26;

        if (verifyCode == index5) {
            var uid = index1 + index2 * 26 + index3 * 26 * 26 + index4 * 26 * 26 * 26 + 1;
            return uid;
        } else {
            return false;
        }
    };
    //uid生成推荐码 注意，修改时usuz_srv也要改
    var uidToCode = this.uidToCode = function (uid) {
        var set ='qwertyuiopasdfghjklzxcvbnm';
        var index1;
        var index2;
        var index3;
        var index4;
        var index5;
        var merchant = parseInt(uid)-1;

        index1 = merchant % 26;
        index2 = parseInt(merchant/26);
        index3 = 0;
        index4 = 0;

        if(index2 >= 26) {
            index3 = parseInt(index2 / 26);
            index2 = index2 % 26;
            if(index3 >= 26) {
                index4 = parseInt(index3 / 26);
                index3 = index3 % 26;
            }
        }
        index5 = (index1 + index2 + index3 + index4) % 26;
        var str = set[index5] + set[index1] + set[index2] + set[index3] + set[index4];
        return str;
    }
    var paramAll = this.paramAll= function(req) {
        var parameter = {};
        if (req.params) {
            for (var p in req.params) {
                parameter[p] = req.params[p];
            }
        }
        if (req.body) {
            for (var p in req.body) {
                parameter[p] = req.body[p];
            }
        }
        if (req.query) {
            for (var p in req.query) {
                parameter[p] = req.query[p];
            }
        }
        return parameter;
    };

    //试用期时间是否过期
    var probation = this.probation = function (data) {
        // var now = new Date();
        var regist_time = new Date(data);
        // var day = ( now.getTime() - regist_time.getTime() ) /(1000*60*60*24);
        // var info = {};
        // regist_time = regist_time.getTime() + (1000*60*60*24*3);
        // regist_time = new Date(regist_time);
        // info = (day < 3) ? {can_use:1, probation_time:regist_time} : {can_use:0,probation_time:regist_time};
        var info = {can_use:0,probation_time:regist_time};
        return info ;
    }

    //会员期限是否过期
    var vipEndTime = this.vipEndTime = function (data) {
        var now = new Date();
        var end_time = new Date(data);
        var day = ( end_time.getTime() - now.getTime() ) /(1000*60*60*24);
        var info = {};
        end_time = end_time.getTime();
        end_time = new Date(end_time);
        info = (day > 0) ? {can_use:1, vip_end_time:end_time} : {can_use:0,vip_end_time:end_time};
        return info ;
    }

})();